package com.kdtech.analyse.app;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

public class FlipboardAppAnalyse implements AnalyseNews {

	private static final String[] regex={
		"https://www.flipboard.cn/@flip_featurescn/.*/http.*",
		"https://www.flipboard.cn/@flip_featurescn/.*/http.*/",
	};

	public static HashMap<String, String> getLinks(String url){
		HashMap<String, String> links = new HashMap<String, String>();
		
		String sid = "";
		String authorUsername = "";
		String sourceUrl = "";
		if(url.matches("https://www.flipboard.cn/") || url.matches("https://www.flipboard.cn")){
			url = "https://www.flipboard.cn/@flip_featurescn/%E7%83%AD%E9%97%A8%E5%88%86%E4%BA%AB%E9%9B%86%E9%94%A6-1n606mv3z/";
		}
		
		sid = StringUtils.substringAfterLast(url, "-").replaceAll("/", "");
		authorUsername = StringUtils.substringBetween(url, "@", "/");
		
		//Ajax 拉取 JSON 信息的页面 URL
		String realUrl = "https://www.flipboard.cn/api/section/sid%2F" + sid + "%2F" + authorUsername;
		UrlMeta urlMeta = null;
		if(urlMeta!=null && urlMeta.getHtml()!=null && urlMeta.getHtml().length() > 1000){
			String htmltxt = urlMeta.getHtml();
			JSONArray parse = JSONArray.parseArray(htmltxt);
			for(int i = 0; i < parse.size(); i++){
				JSONObject json = JSONObject.parseObject(parse.getString(i));
				sourceUrl = json.getString("sourceURL");
				if(StringUtils.isNotBlank(sourceUrl)){
					try{
						sourceUrl = URLEncoder.encode(sourceUrl, "ASCII");
					}catch(UnsupportedEncodingException e){
						System.out.println(e);
					}
//					String detailUrl = UrlArgumentTop.FromatUrl(url + "/" + sourceUrl);
//					links.put(detailUrl, "");
				}
			}
		}else{
			return null;
		}
		System.out.println(links);
		
		return links;
	}

	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		String realUrl = "";
		if(url.indexOf("http%")!=-1){
			realUrl = "https://www.flipboard.cn/api/article/http" + StringUtils.substringAfterLast(url, "http");
		}else{
			return null;
		}
//		String htmltxt=CrawlHTML.responseToURL(realUrl).getHtml();
		String htmltxt = null;
		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		if(StringUtils.isNotBlank(htmltxt) && htmltxt.length()>1000){
			JSONObject jObj = JSONObject.parseObject(htmltxt).getJSONObject("content");
			title = jObj.getString("title");
			date = jObj.getLong("created_date");
			if(date==null)
				date = DateUtils.matchDate(url);
			content = jObj.getString("rss");
			if(StringUtils.isBlank(content)){
				content = jObj.getJSONObject("article").getString("markup");
			}
		}
		
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	/*
	public static void main(String[] args) throws UnsupportedEncodingException {
		String url="https://www.flipboard.cn/@flip_featurescn/%E7%83%AD%E9%97%A8%E5%88%86%E4%BA%AB%E9%9B%86%E9%94%A6-1n606mv3z/http%3A%2F%2Fnews.sina.com.cn%2Fs%2Fwh%2F2015-11-10%2Fdoc-ifxkmrvp5318935.shtml";
//		FlipboardAppAnalyse.getLinks(url);
		FlipboardAppAnalyse analyse=new FlipboardAppAnalyse();
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(UrlArgumentTop.FromatUrl(url));
		System.out.println(analyse.isDetailPage(url));
		UrlMeta meta=CrawlHTML.responseToURL(url);
		System.out.println(analyse.parserHtml(meta));
	}

	*/

	
}

